package visiopuce.ui;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.bindings.keys.ParseException;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.SimpleContentProposalProvider;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.nebula.widgets.formattedtext.DateFormatter;
import org.eclipse.nebula.widgets.formattedtext.FormattedText;
import org.eclipse.nebula.widgets.formattedtext.NumberFormatter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import visiopuce.ApplicationContexte;
import visiopuce.objets.Bande;
import visiopuce.objets.Biomedecine;
import visiopuce.objets.Recherche;
import visiopuce.objets.TypeAnalyse;
import visiopuce.objets.TypePrelevement;
import visiopuce.objets.TypeRendu;
import visiopuce.objets.Utilisateur;
import visiopuce.service.AnalyseServiceImpl;
import visiopuce.service.PersonneServiceImpl;
import visiopuce.service.PrelevementServiceImpl;
import visiopuce.service.ReferentielServiceImpl;
import visiopuce.service.UtilisateurServiceImpl;
import visiopuce.ui.providers.BandeLabelProvider;
import visiopuce.ui.providers.TypeAnalyseLabelProvider;
import visiopuce.ui.providers.TypeIndicationLabelProvider;
import visiopuce.ui.providers.TypePrelevementLabelProvider;
import visiopuce.ui.providers.TypeRenduLabelProvider;
import visiopuce.ui.providers.UtilisateurLabelProvider;

public class RechercheDossierDialog extends Dialog {

	private Button btnPrnatal;
	private Button btnPostnatal;
	private Button btnPucesPre;
	private Button btnPucesPost;
	private Button btnFoeto;
	private Button btnPucesFoeto;
	private Button btnBanques;
	private Button btnTousLesSecteurs;
	private Text tNom;
	private Text tPrenom;
	private Text tNumHospit;
	private Text tNumero;
	private Button btnRadioNonRendu;
	private Button btnCheckUrgent;
	private Button btnRadioRendu;
	private Composite composite;
	private ComboViewer comboTypeIndication;
	private ComboViewer comboTypeEchantillon;
	private ComboViewer comboTypeAnalyse;
	private ComboViewer comboPrescripteur;
	private Combo comboUFPresc;
	private ComboViewer comboSignataire;
	private Combo comboResultat;
	private ComboViewer comboAnomalie;
	private Combo comboUFExec;
	private Button btnRadioUniquementRecherche;
	private Button btnRadioUniquementNonRecherche;
	private Button btnRadioTousRecherche;
	private Button btnRadioUniquementNonDPI;
	private Button btnRadioTousDPI;
	private Button btnRadioUniquementDPI;
	private FormattedText tDateResutlatDebut;
	private FormattedText tDateResutlatFin;
	private FormattedText tDateTechniqueDebut;
	private FormattedText tDateTechniqueFin;
	private FormattedText tDatePrelevementDebut;
	private FormattedText tDatePrelevementFin;
	private FormattedText tDateNaissanceDebut;
	private FormattedText tDateNaissanceFin;
	private String[] listeUFExec;
	private String[] listeUFPresc;
	private String[] listeResultats;
	private Button[] btnCheckChromosome = new Button[24];
	private KeyStroke ks = null;
	private ContentProposalAdapter adapterNumero;
	private ContentProposalAdapter adapterPrenom;
	private ContentProposalAdapter adapterNom;
	private Composite compositeRegion;
	private ComboViewer comboBande;
	private Combo comboChr;
	private FormattedText tDebut;
	private FormattedText tFin;

	public RechercheDossierDialog(Shell parentShell) {

		super(parentShell);
		try {
			ks = KeyStroke.getInstance("Ctrl+Space");

			setShellStyle(SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
			setBlockOnOpen(true);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText("Rechercher un dossier");
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout layout = new GridLayout(2, false);
		layout.marginRight = 5;
		layout.marginLeft = 10;
		container.setLayout(layout);

		Label lblNumroDhospitalisationentr = new Label(container, SWT.NONE);
		lblNumroDhospitalisationentr.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNumroDhospitalisationentr.setText("Num\u00E9ro d'hospitalisation (entr\u00E9 \u00E0 la main ou scann\u00E9) :");

		tNumHospit = new Text(container, SWT.BORDER);
		tNumHospit.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Label lblParSecteur = new Label(container, SWT.NONE);
		lblParSecteur.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblParSecteur.setText("Par secteur");
		new Label(container, SWT.NONE);

		Group group = new Group(container, SWT.NONE);
		group.setLayout(new GridLayout(3, false));

		btnTousLesSecteurs = new Button(group, SWT.RADIO);
		btnTousLesSecteurs.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		btnTousLesSecteurs.setSelection(true);
		btnTousLesSecteurs.setText("tous secteurs confondus");
		new Label(group, SWT.NONE);
		new Label(group, SWT.NONE);

		btnPrnatal = new Button(group, SWT.RADIO);
		btnPrnatal.setText(ApplicationContexte.SECTEUR_PRENATAL);

		btnPucesPre = new Button(group, SWT.RADIO);
		btnPucesPre.setText(ApplicationContexte.SECTEUR_PUCE_PRENATAL);

		btnBanques = new Button(group, SWT.RADIO);
		btnBanques.setText(ApplicationContexte.SECTEUR_BANQUE);

		btnPostnatal = new Button(group, SWT.RADIO);
		btnPostnatal.setText(ApplicationContexte.SECTEUR_POSTNATAL);

		btnPucesPost = new Button(group, SWT.RADIO);
		btnPucesPost.setText(ApplicationContexte.SECTEUR_PUCE_POSTNATAL);
		new Label(group, SWT.NONE);

		btnFoeto = new Button(group, SWT.RADIO);
		btnFoeto.setText(ApplicationContexte.SECTEUR_FOETO);

		btnPucesFoeto = new Button(group, SWT.RADIO);
		btnPucesFoeto.setText(ApplicationContexte.SECTEUR_PUCE_FOETO);
		new Label(group, SWT.NONE);
		new Label(container, SWT.NONE);

		Label lblParNom = new Label(container, SWT.NONE);
		lblParNom.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblParNom.setText("Nom (Ctrl+espace pour auto compl\u00E9ter):");

		tNom = new Text(container, SWT.BORDER);
		tNom.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		SimpleContentProposalProvider scp = new SimpleContentProposalProvider(null);
		adapterNom = new ContentProposalAdapter(tNom, new TextContentAdapter(), scp, ks, null);
		adapterNom.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);

		tNom.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent ke) {
				if (ke.keyCode == 262144) {
					setAutoCompletionNom(tNom.getText());
				}
			}

		});

		Label lblPrnom = new Label(container, SWT.NONE);
		lblPrnom.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblPrnom.setText("Pr\u00E9nom (Ctrl+espace pour auto compl\u00E9ter):");

		tPrenom = new Text(container, SWT.BORDER);
		tPrenom.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		SimpleContentProposalProvider scpPrenom = new SimpleContentProposalProvider(null);
		adapterPrenom = new ContentProposalAdapter(tPrenom, new TextContentAdapter(), scpPrenom, ks, null);
		adapterPrenom.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);

		tPrenom.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent ke) {
				if (ke.keyCode == 262144) {
					setAutoCompletionPrenom(tPrenom.getText());
				}
			}
		});

		//
		// Label lblParDateDe = new Label(container, SWT.NONE);
		// lblParDateDe.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
		// false, 1, 1));
		// lblParDateDe.setText("Par date de naissance :");
		//
		// DateTime dateTime = new DateTime(container, SWT.BORDER);

		Label lblParNumeroPrelevement = new Label(container, SWT.NONE);
		GridData gd_lblParNumeroPrelevement = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
		gd_lblParNumeroPrelevement.horizontalIndent = 1;
		lblParNumeroPrelevement.setLayoutData(gd_lblParNumeroPrelevement);
		lblParNumeroPrelevement.setText("Par num\u00E9ro de pr\u00E9l\u00E8vement (Ctrl+espace pour auto compl\u00E9ter):");

		tNumero = new Text(container, SWT.BORDER);
		tNumero.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		SimpleContentProposalProvider scpNum = new SimpleContentProposalProvider(null);
		adapterNumero = new ContentProposalAdapter(tNumero, new TextContentAdapter(), scpNum, ks, null);
		adapterNumero.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);

		tNumero.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent ke) {
				if (ke.keyCode == 262144) {
					setAutoCompletionNumero(tNumero.getText());
				}
			}

		});

		Composite composite_1 = new Composite(container, SWT.NONE);
		composite_1.setLayout(new GridLayout(1, false));

		btnRadioNonRendu = new Button(composite_1, SWT.RADIO);
		btnRadioNonRendu.setText("Rechercher uniquement les analyses non rendues");

		btnRadioRendu = new Button(composite_1, SWT.RADIO);
		btnRadioRendu.setText("Rechercher uniquement les analyses rendues");

		btnCheckUrgent = new Button(composite_1, SWT.CHECK);
		btnCheckUrgent.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		btnCheckUrgent.setText("Rechercher uniquement les analyses urgentes non rendues");
		new Label(container, SWT.NONE);

		// Label lblPourLesPuces = new Label(container, SWT.NONE);
		// lblPourLesPuces.setFont(SWTResourceManager.getFont("Segoe UI", 9,
		// SWT.BOLD));
		// lblPourLesPuces.setText("Pour les puces");
		// new Label(container, SWT.NONE);
		//
		// Label lblParFeuille = new Label(container, SWT.NONE);
		// lblParFeuille.setText("Par num\u00E9ro de feuille de travail :");
		// lblParFeuille.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
		// false, false, 1, 1));
		//
		// ComboViewer comboViewerFeuille = new ComboViewer(container,
		// SWT.NONE);
		// Combo comboParFeuille = comboViewerFeuille.getCombo();
		// comboParFeuille.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
		// true, false, 1, 1));
		//
		// Label lblParModleDe = new Label(container, SWT.NONE);
		// lblParModleDe.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
		// false, false, 1, 1));
		// lblParModleDe.setText("Par mod\u00E8le de puce :");
		//
		// ComboViewer comboViewer = new ComboViewer(container, SWT.NONE);
		// Combo comboParModelePuce = comboViewer.getCombo();
		// comboParModelePuce.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
		// true, false, 1, 1));
		//
		ExpandBar expandBar = new ExpandBar(container, SWT.NONE);
		GridData gd_expandBar = new GridData(SWT.LEFT, SWT.FILL, false, false, 2, 1);
		gd_expandBar.heightHint = 608;
		gd_expandBar.widthHint = 1017;
		expandBar.setLayoutData(gd_expandBar);

		ExpandItem xpndtmRechercheAvance = new ExpandItem(expandBar, SWT.NONE);
		xpndtmRechercheAvance.setExpanded(false);
		xpndtmRechercheAvance.setText("Recherche avanc\u00E9e");

		composite = new Composite(expandBar, SWT.NONE);
		xpndtmRechercheAvance.setControl(composite);
		xpndtmRechercheAvance.setHeight(530);
		composite.setLayout(new GridLayout(2, false));

		Label lblPar = new Label(composite, SWT.NONE);
		lblPar.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblPar.setText("Par indication");

		comboTypeIndication = new ComboViewer(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboTypeIndication.setContentProvider(ArrayContentProvider.getInstance());
		comboTypeIndication.setLabelProvider(new TypeIndicationLabelProvider());
		comboTypeIndication.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		ArrayList<Biomedecine> listTypeIndication = ReferentielServiceImpl.getInstance().getTypeIndication("");
		comboTypeIndication.setInput(listTypeIndication);

		Label lblParTypeDe = new Label(composite, SWT.NONE);
		lblParTypeDe.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblParTypeDe.setText("Par type d'\u00E9chantillon");

		comboTypeEchantillon = new ComboViewer(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboTypeEchantillon.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		comboTypeEchantillon.setContentProvider(ArrayContentProvider.getInstance());
		comboTypeEchantillon.setLabelProvider(new TypePrelevementLabelProvider());
		ArrayList<TypePrelevement> listTypePrelevements = ReferentielServiceImpl.getInstance().getTypePrelevement();
		// fill the list with ComboValue objects
		comboTypeEchantillon.setInput(listTypePrelevements);

		Label lblParTypeDanalyse = new Label(composite, SWT.NONE);
		lblParTypeDanalyse.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblParTypeDanalyse.setText("Par type d'analyse");

		comboTypeAnalyse = new ComboViewer(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboTypeAnalyse.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		comboTypeAnalyse.setContentProvider(ArrayContentProvider.getInstance());
		comboTypeAnalyse.setLabelProvider(new TypeAnalyseLabelProvider());
		ArrayList<TypeAnalyse> listTypeAnalyses = ReferentielServiceImpl.getInstance().getTypeAnalyse(null);
		comboTypeAnalyse.setInput(listTypeAnalyses);

		Label lblParRangement = new Label(composite, SWT.NONE);
		lblParRangement.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblParRangement.setText("Par prescripteur");

		comboPrescripteur = new ComboViewer(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboPrescripteur.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		comboPrescripteur.setContentProvider(ArrayContentProvider.getInstance());
		comboPrescripteur.setLabelProvider(new UtilisateurLabelProvider());
		// fill the list with ComboValue objects
		ArrayList<Utilisateur> listPrescipteurs = UtilisateurServiceImpl.getInstance().getPrescripteurs();
		comboPrescripteur.setInput(listPrescipteurs);

		Label lblParUfPrescripteur = new Label(composite, SWT.NONE);
		lblParUfPrescripteur.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblParUfPrescripteur.setText("Par UF prescripteur");

		comboUFPresc = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboUFPresc.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		listeUFPresc = PrelevementServiceImpl.getInstance().getUFPrescs();
		comboUFPresc.setItems(listeUFPresc);

		Label lblParUfExecutant = new Label(composite, SWT.NONE);
		lblParUfExecutant.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblParUfExecutant.setText("Par UF executant");

		comboUFExec = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboUFExec.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		listeUFExec = AnalyseServiceImpl.getInstance().getUFExecs();
		comboUFExec.setItems(listeUFExec);

		Label lblParSignataire = new Label(composite, SWT.NONE);
		lblParSignataire.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblParSignataire.setText("Par signataire");

		comboSignataire = new ComboViewer(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboSignataire.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		comboSignataire.setContentProvider(ArrayContentProvider.getInstance());
		comboSignataire.setLabelProvider(new UtilisateurLabelProvider());
		// fill the list with ComboValue objects
		ArrayList<Utilisateur> listBiologistes = UtilisateurServiceImpl.getInstance().getBiologisteByTypeAnalyse(0);
		comboSignataire.setInput(listBiologistes);

		Label lblParRsultat = new Label(composite, SWT.NONE);
		lblParRsultat.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblParRsultat.setText("Par r\u00E9sultat");

		comboResultat = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboResultat.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		listeResultats = new String[] { "Non rendu", "Echec", "Annulé", "Normal", "Anormal", "Anormal équilibré", "Anormal déséquilibré" };
		comboResultat.setItems(listeResultats);

		Label lblParAnomalie = new Label(composite, SWT.NONE);
		lblParAnomalie.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblParAnomalie.setText("Par anomalie");

		comboAnomalie = new ComboViewer(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboAnomalie.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		comboAnomalie.setContentProvider(ArrayContentProvider.getInstance());
		comboAnomalie.setLabelProvider(new TypeRenduLabelProvider());
		ArrayList<TypeRendu> listeTypeRenduStr = ReferentielServiceImpl.getInstance().getTypeRendu(-1);
		listeTypeRenduStr.add(0, new TypeRendu(""));
		comboAnomalie.setInput(listeTypeRenduStr);

		Group lblParChromosomeAtteint = new Group(composite, SWT.NONE);
		lblParChromosomeAtteint.setText("Par chromosome(s) atteint(s)");
		lblParChromosomeAtteint.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
		lblParChromosomeAtteint.setLayout(new GridLayout(24, true));

		String[] chrs = ApplicationContexte.chr;
		int i = 0;
		for (String chr : chrs) {
			btnCheckChromosome[i] = new Button(lblParChromosomeAtteint, SWT.CHECK);
			btnCheckChromosome[i].setText(chr);
			i++;
		}

		Composite composite_2 = new Composite(composite, SWT.NONE);
		composite_2.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
		composite_2.setLayout(new GridLayout(4, false));

		Label lblDateDeNaissance = new Label(composite_2, SWT.NONE);
		lblDateDeNaissance.setText("Date de naissance entre ");

		tDateNaissanceDebut = new FormattedText(composite_2, SWT.BORDER);
		GridData gd_DateNaissanceDebut = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_DateNaissanceDebut.widthHint = 75;
		tDateNaissanceDebut.getControl().setLayoutData(gd_DateNaissanceDebut);
		tDateNaissanceDebut.setFormatter(new DateFormatter("dd/MM/yyyy"));

		Label lblEt = new Label(composite_2, SWT.NONE);
		lblEt.setText("et");

		tDateNaissanceFin = new FormattedText(composite_2, SWT.BORDER);
		GridData gd_DateNaissanceFin = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_DateNaissanceFin.widthHint = 75;
		tDateNaissanceFin.getControl().setLayoutData(gd_DateNaissanceFin);
		tDateNaissanceFin.setFormatter(new DateFormatter("dd/MM/yyyy"));

		Composite composite_3 = new Composite(composite, SWT.NONE);
		composite_3.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
		composite_3.setLayout(new GridLayout(4, false));

		Label label_1 = new Label(composite_3, SWT.NONE);
		label_1.setText("Date de resultat entre ");

		tDateResutlatDebut = new FormattedText(composite_3, SWT.BORDER);
		GridData gd_DateResutlatDebut = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_DateResutlatDebut.widthHint = 75;
		tDateResutlatDebut.getControl().setLayoutData(gd_DateResutlatDebut);
		tDateResutlatDebut.setFormatter(new DateFormatter("dd/MM/yyyy"));

		Label label_2 = new Label(composite_3, SWT.NONE);
		label_2.setText("et");

		tDateResutlatFin = new FormattedText(composite_3, SWT.BORDER);
		GridData gd_DateResutlatFin = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_DateResutlatFin.widthHint = 75;
		tDateResutlatFin.getControl().setLayoutData(gd_DateResutlatFin);
		tDateResutlatFin.setFormatter(new DateFormatter("dd/MM/yyyy"));

		Composite composite_4 = new Composite(composite, SWT.NONE);
		composite_4.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
		composite_4.setLayout(new GridLayout(4, false));

		Label lblDateDeTechnique = new Label(composite_4, SWT.NONE);
		lblDateDeTechnique.setText("Date de technique entre ");

		tDateTechniqueDebut = new FormattedText(composite_4, SWT.BORDER);
		GridData gd_DateTechniqueDebut = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_DateTechniqueDebut.widthHint = 75;
		tDateTechniqueDebut.getControl().setLayoutData(gd_DateTechniqueDebut);
		tDateTechniqueDebut.setFormatter(new DateFormatter("dd/MM/yyyy"));

		Label label_5 = new Label(composite_4, SWT.NONE);
		label_5.setText("et");

		tDateTechniqueFin = new FormattedText(composite_4, SWT.BORDER);
		GridData gd_DateTechniqueFin = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_DateTechniqueFin.widthHint = 75;
		tDateTechniqueFin.getControl().setLayoutData(gd_DateTechniqueFin);
		tDateTechniqueFin.setFormatter(new DateFormatter("dd/MM/yyyy"));

		Composite composite_5 = new Composite(composite, SWT.NONE);
		composite_5.setLayout(new GridLayout(4, false));
		composite_5.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));

		Label lblDateDePrlvement = new Label(composite_5, SWT.NONE);
		lblDateDePrlvement.setText("Date de pr\u00E9l\u00E8vement ou r\u00E9ception entre ");

		tDatePrelevementDebut = new FormattedText(composite_5, SWT.BORDER);
		GridData gd_tDatePrelevementDebut = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_tDatePrelevementDebut.widthHint = 75;
		tDatePrelevementDebut.getControl().setLayoutData(gd_tDatePrelevementDebut);
		tDatePrelevementDebut.setFormatter(new DateFormatter("dd/MM/yyyy"));

		Label label_3 = new Label(composite_5, SWT.NONE);
		label_3.setText("et");

		tDatePrelevementFin = new FormattedText(composite_5, SWT.BORDER);
		GridData gd_tDatePrelevementFin = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_tDatePrelevementFin.widthHint = 75;
		tDatePrelevementFin.getControl().setLayoutData(gd_tDatePrelevementFin);
		tDatePrelevementFin.setFormatter(new DateFormatter("dd/MM/yyyy"));

		Group gpUniquementRecherche = new Group(composite, SWT.NONE);
		gpUniquementRecherche.setLayout(new GridLayout(1, false));
		btnRadioUniquementRecherche = new Button(gpUniquementRecherche, SWT.RADIO);
		btnRadioUniquementRecherche.setText("Rechercher uniquement les pr\u00E9l\u00E8vements recherche");
		btnRadioUniquementRecherche.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));

		btnRadioUniquementNonRecherche = new Button(gpUniquementRecherche, SWT.RADIO);
		btnRadioUniquementNonRecherche.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
		btnRadioUniquementNonRecherche.setText("Rechercher uniquement pr\u00E9l\u00E8vements NON recherche");

		btnRadioTousRecherche = new Button(gpUniquementRecherche, SWT.RADIO);
		btnRadioTousRecherche.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
		btnRadioTousRecherche.setText("Rechercher tous les prélèvement recherche et NON recherche");

		Group gpUniquementDPI = new Group(composite, SWT.NONE);
		gpUniquementDPI.setLayout(new GridLayout(1, false));
		btnRadioUniquementDPI = new Button(gpUniquementDPI, SWT.RADIO);
		btnRadioUniquementDPI.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
		btnRadioUniquementDPI.setText("Rechercher uniquement les pr\u00E9l\u00E8vements DPI");

		btnRadioUniquementNonDPI = new Button(gpUniquementDPI, SWT.RADIO);
		btnRadioUniquementNonDPI.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
		btnRadioUniquementNonDPI.setText("Rechercher uniquement les pr\u00E9l\u00E8vements NON DPI");

		btnRadioTousDPI = new Button(gpUniquementDPI, SWT.RADIO);
		btnRadioTousDPI.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
		btnRadioTousDPI.setText("Rechercher tous les prélèvement DPI et NON DPI");
		composite.pack();

		return container;
	}

	// override method to use "Login" as label for the OK button
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, "Rechercher", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "Annuler", false);
	}

	@Override
	protected Point getInitialSize() {
		return new Point(1047, 1011);
	}

	@Override
	protected void okPressed() {
		Recherche recherche = new Recherche();
		recherche.setSecteur(null);
		if (!tNom.getText().isEmpty()) {
			recherche.setNom(tNom.getText());
		}
		if (!tPrenom.getText().isEmpty()) {
			recherche.setPrenom(tPrenom.getText());
		}
		if (!tNumero.getText().isEmpty()) {
			recherche.setNumeroPrelevement(tNumero.getText());
		}
		if (!tNumHospit.getText().isEmpty()) {
			recherche.setNumeroHospit(tNumHospit.getText());
		}
		if (btnBanques.getSelection()) {
			recherche.setSecteur(ReferentielServiceImpl.getInstance().getSecteurByNom(btnBanques.getText()));
		} else {
			if (btnPrnatal.getSelection()) {
				recherche.setSecteur(ReferentielServiceImpl.getInstance().getSecteurByNom(btnPrnatal.getText()));
			} else {
				if (btnPostnatal.getSelection()) {
					recherche.setSecteur(ReferentielServiceImpl.getInstance().getSecteurByNom(btnPostnatal.getText()));
				} else {
					if (btnPucesPost.getSelection()) {
						recherche.setSecteur(ReferentielServiceImpl.getInstance().getSecteurByNom(btnPucesPost.getText()));
					} else {
						if (btnPucesPre.getSelection()) {
							recherche.setSecteur(ReferentielServiceImpl.getInstance().getSecteurByNom(btnPucesPre.getText()));
						} else {
							if (btnPucesFoeto.getSelection()) {
								recherche.setSecteur(ReferentielServiceImpl.getInstance().getSecteurByNom(btnPucesFoeto.getText()));
							} else {
								if (btnFoeto.getSelection()) {
									recherche.setSecteur(ReferentielServiceImpl.getInstance().getSecteurByNom(btnFoeto.getText()));
								}
							}
						}
					}
				}
			}
		}
		if (btnRadioRendu.getSelection()) {
			recherche.setQueRendu(1);
		} else if (btnRadioNonRendu.getSelection()) {
			recherche.setQueRendu(0);
		} else {
			recherche.setQueRendu(-1);
		}
		if (btnRadioUniquementRecherche.getSelection()) {
			recherche.setQueRecherche(1);
		} else if (btnRadioUniquementNonRecherche.getSelection()) {
			recherche.setQueRecherche(0);
		} else {
			recherche.setQueRecherche(-1);
		}
		if (btnRadioUniquementDPI.getSelection()) {
			recherche.setQueDPI(1);
		} else if (btnRadioUniquementNonDPI.getSelection()) {
			recherche.setQueDPI(0);
		} else {
			recherche.setQueDPI(-1);
		}
		if (tDateResutlatDebut != null) {
			recherche.setDateResultatDebut((Date) tDateResutlatDebut.getValue());
		}
		if (tDateResutlatFin != null) {
			recherche.setDateResultatFin((Date) tDateResutlatFin.getValue());
		}
		if (tDateTechniqueDebut != null) {
			recherche.setDateTechniqueDebut((Date) tDateTechniqueDebut.getValue());
		}
		if (tDateTechniqueFin != null) {
			recherche.setDateTechniqueFin((Date) tDateTechniqueFin.getValue());
		}
		if (tDatePrelevementDebut != null) {
			recherche.setDatePrelevementDebut((Date) tDatePrelevementDebut.getValue());
		}
		if (tDatePrelevementFin != null) {
			recherche.setDatePrelevementFin((Date) tDatePrelevementFin.getValue());
		}
		if (tDateNaissanceDebut != null) {
			recherche.setDateNaissanceDebut((Date) tDateNaissanceDebut.getValue());
		}
		if (tDateNaissanceFin != null) {
			recherche.setDateNaissanceFin((Date) tDateNaissanceFin.getValue());
		}
		ISelection selectionTypeIndic = comboTypeIndication.getSelection();
		if (!selectionTypeIndic.isEmpty()) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selectionTypeIndic;
			recherche.setIndication((Biomedecine) structuredSelection.getFirstElement());
		}
		ISelection selectionTypeEchant = comboTypeEchantillon.getSelection();
		if (!selectionTypeEchant.isEmpty()) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selectionTypeEchant;
			recherche.setTypePrelevement((TypePrelevement) structuredSelection.getFirstElement());
		}
		ISelection selectionTypeAna = comboTypeAnalyse.getSelection();
		if (!selectionTypeAna.isEmpty()) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selectionTypeAna;
			recherche.setTypeAnalyse((TypeAnalyse) structuredSelection.getFirstElement());
		}
		ISelection selectionTypeRendu = comboAnomalie.getSelection();
		if (!selectionTypeRendu.isEmpty()) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selectionTypeRendu;
			recherche.setTypeRendu((TypeRendu) structuredSelection.getFirstElement());
		}
		int selectUFExec = comboUFExec.getSelectionIndex();
		if (selectUFExec > 0) {
			recherche.setUFExec(listeUFExec[selectUFExec]);
		}
		int selectUFPresc = comboUFPresc.getSelectionIndex();
		if (selectUFPresc > 0) {
			recherche.setUFPresc(listeUFPresc[selectUFPresc]);
		}
		int selectResultat = comboResultat.getSelectionIndex();
		if (selectResultat > 0) {
			recherche.setResultat(listeResultats[selectResultat]);
		}
		ISelection selectionMedecin = comboPrescripteur.getSelection();
		if (!selectionMedecin.isEmpty()) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selectionMedecin;
			recherche.setPrescripteur((Utilisateur) structuredSelection.getFirstElement());
		}
		ISelection selectionBiologiste = comboSignataire.getSelection();
		if (!selectionBiologiste.isEmpty()) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selectionBiologiste;
			recherche.setSignataire((Utilisateur) structuredSelection.getFirstElement());
		}

		String[] chrs = ApplicationContexte.chr;
		List<String> chromosomes = new ArrayList();
		;
		for (int i = 0; i < chrs.length; i++) {
			if (btnCheckChromosome[i].getSelection()) {
				chromosomes.add(chrs[i]);
			}
		}
		if (chromosomes.size() > 0) {
			recherche.setChromosomes(chromosomes);
		}

		ApplicationContexte.getInstance().setRechercheEnCours(recherche);
		super.okPressed();
	}

	private void setAutoCompletionNumero(String txt) {
		try {
			String[] defaultProposals = PrelevementServiceImpl.getInstance().getNumeroFromNumero(txt);
			if (defaultProposals.length > 0) {
				((SimpleContentProposalProvider) adapterNumero.getContentProposalProvider()).setProposals(defaultProposals);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void setAutoCompletionNom(String txt) {
		try {
			String[] defaultProposals = PersonneServiceImpl.getInstance().getNomFromString(txt);
			if (defaultProposals.length > 0) {
				((SimpleContentProposalProvider) adapterNom.getContentProposalProvider()).setProposals(defaultProposals);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void setAutoCompletionPrenom(String txt) {
		try {
			String[] defaultProposals = PersonneServiceImpl.getInstance().getPrenomFromNomPrenom(tNom.getText(), txt);
			if (defaultProposals.length > 0) {
				((SimpleContentProposalProvider) adapterPrenom.getContentProposalProvider()).setProposals(defaultProposals);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
